<template>
  <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    :rules="rules"
    label-width="180px"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    <template v-for="(opt, idx) in options" :key="idx">
      <el-form-item :label="opt.label" :prop="opt.prop" :rules="opt.rules">
        <el-input
          v-if="opt.type === 'input' || opt.type === 'textarea'"
          :type="opt.type"
          v-model="ruleForm[opt.prop]"
          v-bind="{ ...opt.attrs }"
        />
        <!-- //End input textarea -->

        <el-select
          v-if="opt.type === 'select'"
          v-model="ruleForm[opt.prop]"
          :placeholder="opt.placeholder || ''"
          v-bind="{ ...opt.attrs }"
        >
          <el-option
            v-for="(item, key) in opt.options"
            :label="item.label"
            :value="item.value"
            :key="key"
          />
        </el-select>
        <!-- //End input select -->

        <el-checkbox-group
          v-if="opt.type === 'checkbox'"
          v-model="ruleForm[opt.prop]"
          v-bind="{ ...opt.attrs }"
        >
          <el-checkbox
            v-for="(item, key) in opt.options"
            :label="item.label"
            :key="key"
          />
        </el-checkbox-group>
        <!-- //End checkbox -->

        <el-radio-group
          v-if="opt.type === 'radio'"
          v-model="ruleForm[opt.prop]"
          v-bind="{ ...opt.attrs }"
        >
          <el-radio
            v-for="(item, key) in opt.options"
            :key="key"
            :label="item.label"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
        <!-- //End checkbox -->

        <template v-if="opt.type === 'submit'">
          <el-button
            v-for="(btn, key) in opt.options"
            :key="key"
            v-bind="{ ...btn.attrs }"
            @click="onClickForm(ruleFormRef, btn.event)"
          >
            {{ btn.text }}
          </el-button>
        </template>
      </el-form-item>
    </template>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref, defineProps, defineEmits } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'

defineProps({
  options: {
    type: Object
  }
})

const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
let ruleForm = reactive({})

const rules = reactive<FormRules>({
  name: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' }
  ],
  region: [
    {
      required: true,
      message: 'Please select Activity zone',
      trigger: 'change'
    }
  ],
  count: [
    {
      required: true,
      message: 'Please select Activity count',
      trigger: 'change'
    }
  ],
  date1: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a date',
      trigger: 'change'
    }
  ],
  date2: [
    {
      type: 'date',
      required: true,
      message: 'Please pick a time',
      trigger: 'change'
    }
  ],
  type: [
    {
      type: 'array',
      required: true,
      message: 'Please select at least one activity type',
      trigger: 'change'
    }
  ],
  resource: [
    {
      required: true,
      message: 'Please select activity resource',
      trigger: 'change'
    }
  ],
  desc: [
    { required: true, message: 'Please input activity form', trigger: 'blur' }
  ]
})

const emits = defineEmits([])

// 事件透传回父组件
const onClickForm = async (formEl: FormInstance | undefined, type: string) => {
  if (!formEl) return
  if (type === 'submit') {
    await formEl.validate((valid, fields) => {
      if (valid) {
        emits(type, ruleForm)
      } else {
        console.log('error submit!', fields)
      }
    })
  } else if (type === 'reset') {
    formEl.resetFields()
  } else {
    // 除了 提交和重置表单事件，其他的事件都直接透传回去
    emits(type, ruleForm)
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .el-select {
    width: 100%;
  }
}
</style>
